%% GRNN Function Approximation
% Modified demo demogrn1.m to learn the simulated arm data.
% This demonstration uses functions NEWGRNN and SIM.
%


for spread = 0.1:.05:0.3

    disp(['Spread = ' num2str(spread)]);

%%
% Here are eight data points of a function we would like to fit.  The functions
% inputs P should result in target outputs T.

P = samples(1:1500-1,1:2:end)';
T = samples(2:1500,1:2:3)';

figure(1)
plot3(samples(:,1),samples(:,3),samples(:,5));xlabel('theta'),ylabel('dtheta');zlabel('tau')

%%
% We use NEWGRNN to create a generalized regression network.  We use a SPREAD
% slightly lower than 1, the distance between input values, in order, to get a
% function that fits individual data points fairly closely.  A smaller spread
% would fit data better but be less smooth.

net = newgrnn(P,T,spread);
A = sim(net,P);


figure(2)
plot([A',T']);
title('original data');
mean(abs(A-T),2)*180/pi

%% 
% Test on new data not used during training.

P = samples(1501:end-1,1:2:end)';
T = samples(1501+1:end,1:2:3)';

A = sim(net,P);

figure(3)
plot([A',T']);
title('novel data');
mean(abs(A-T),2)*180/pi


end
%%
% We can use the network to approximate the function at a new input value.

%p = 3.5;

%a = sim(net,p);

%%
%


